package com.eam.controller;

import com.mes.entity.User;
import com.eam.service.EquipmentInspectionService;
import com.mes.utils.GenerateCodeUtil;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("equipmentInspection")
public class EquipmentInspectionController {
	@Autowired
	private EquipmentInspectionService quipmentInspectionService;
	
	//加载点检计划编码
	@ResponseBody
	@RequestMapping(value="initProcessInfo",method=RequestMethod.POST)
	public Message initProcessInfo(HttpServletRequest request){
		Message msg = new Message();
		String orgId = (String)request.getSession().getAttribute("orgId");    //获取当前登录人是几厂的
		
		if(orgId == null){
			msg.setData("Plans",quipmentInspectionService.getEquipmentCheckPlanList());
			
		}else{
			msg.setData("Plans",quipmentInspectionService.getEquipmentCheckPlanLists(orgId));
		}
		
		return msg;
	}
	
	
	//加载设备编码
	@ResponseBody
	@RequestMapping(value="equipmentProcessInfo",method=RequestMethod.POST)
	public Message equipmentProcessInfo(HttpServletRequest request){
		Message msg = new Message();
		String code = request.getParameter("CODE");
		msg.setData("failices",quipmentInspectionService.getEquipmentList(code));
		
		
		return msg;
	}
	
	
	//根据设备编码和计划编码加载设备信息
	@ResponseBody
	@RequestMapping(value="Spotcheckinformation",method=RequestMethod.POST)
	public Message Spotcheckinformation(HttpServletRequest request){
	
		String code = request.getParameter("CODE");
		String plancode = request.getParameter("PLANCODE");
		Message msg = new Message();		
		msg.setData("information",quipmentInspectionService.getinformationList(plancode,code));
		return msg;
		
	}
	
	
	//保存设备点检信息
	@ResponseBody
	@RequestMapping(value="EquipmentPreservation",method=RequestMethod.POST)
	public Message EquipmentPreservation(HttpServletRequest request){
		Message msg = new Message();
		
		try{
			String Notes = request.getParameter("Remarks");    //处理结果的值
			String code = request.getParameter("CODE");				//设备编码
			String plancode = request.getParameter("PLANCODE");		//计划编码
			String values = request.getParameter("CHESTR");			//复选框点检项目的值
			String username = request.getParameter("user");			//责任人
			String cycles = request.getParameter("CYCLES");			//点检周期
			String Radionumbers = request.getParameter("Radios"); 			//单选框点检结果的值	
			String beizhu = request.getParameter("DESCRIPTION");    //备注
			
			String emesruntime = null;
			String emesmaintainplanid = null;
			String facilitiesid =null;
			
			String [] idss = values.split(",");
			String [] Remarks = Notes.split(",");
			String [] Radios = Radionumbers.split(",");
			String [] Descriptions = beizhu.split(",");
			
			int numbers = idss.length;
			
			User user = UserUtil.getLoginUser(request);
			
			String adorgId = user.getOrgId();         //登录人的ad_org_id
			String adclientId = user.getClientId();   //登录人的ad_client_id
			String userIdtime = user.getId();			//登录人的ad_user_id		
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			String orgId = (String)request.getAttribute("orgId");    //获取当前登录人是几厂的
 
			
			Date time  =sdf.parse(sdf.format(new Date())); //获取当前系统时间
			
			List<Map<String, Object>> ListUser = quipmentInspectionService.getEquipmentputCreateby(userIdtime);
			
			String createBy = ListUser.get(0).get("CREATEDBY").toString();  //登录人的CreateBy
			
			String updatedBy = ListUser.get(0).get("UPDATEDBY").toString(); //登录人的updateBy
			
			Map<String, Object> userIds = quipmentInspectionService.getEquipmentuserId(username);
			
			
			for(int i = 0 ; i< numbers ; i++){
					String xmcode = idss[i];
				List<Map<String, Object>> detailsids  = quipmentInspectionService.getEquipmentput(xmcode,plancode,code); //查出点检项目的ID;
				
				String id = detailsids.get(0).get("XMID").toString();
				
				String RemarksNames = "";
				
				if(Remarks.length == 0){
					
				}else{
					for(int o = 0 ; o<Remarks.length ; o++){
						if(o == i){
							RemarksNames = Remarks[o];
						}
					}
				}
				
				String Description = "";
				
				if(Descriptions.length == 0){
					
				}else{
					for(int o = 0 ; o<Descriptions.length ; o++){
						if(o == i){
							Description = Descriptions[o];
						}
					}
				}
				
				String Radio = Radios[i];
				
				String status = "OK";
				
				quipmentInspectionService.SaveEquipmentPlanDetails(status,id,RemarksNames,Description);
				quipmentInspectionService.SaveEquipmentPlanDetailsCheckResult(Radio, id);
				quipmentInspectionService.SaveEquipmentuserDetailsCheckResult(userIdtime, id);
				Map<String, Object> Totals = quipmentInspectionService.getcountinformationList(plancode,code);
				
				String Total = Totals.get("NUMBERS").toString();					
				
				int a = Integer.parseInt(Total);
				
				String ISACTIVE = "Y";
				
				List<Map<String, Object>> Allresults =  quipmentInspectionService.getAllEquipemt(id);
				
				facilitiesid = Allresults.get(0).get("FACILITIESID").toString();						//设备ID
				
				emesmaintainplanid = Allresults.get(0).get("EmesFACILITIESRUNTIMELOGID").toString();	//计划ID
				
				emesruntime = Allresults.get(0).get("EmesRUNTIME").toString();							//点检项目ID
				
				String recordCode = GenerateCodeUtil.getCode("Eam_Check_Result", "code");  //设备点检记录编码
				
				quipmentInspectionService.SavaEquipmentputInsert(Radio,facilitiesid,RemarksNames,userIdtime,recordCode,emesmaintainplanid,emesruntime,adorgId,adclientId,createBy,updatedBy,ISACTIVE,time); //将点检信息存入点检记录表中
				
				quipmentInspectionService.getEquipmentputList(id);
			}
			
			
		}catch(Exception e){
			msg.setData("results","false");
			e.printStackTrace();
		}
		
		
		return msg;
	}
	

	
	
}
